//
//  UIViewController+JJ_MJ.m
//  WOEWO
//
//  Created by huangjingjin on 2017/4/5.
//  Copyright © 2017年 BPMAC. All rights reserved.
//

#import "UIViewController+MJRefresh.h"

@implementation UIViewController (MJRefresh)

static char curPageKey;
- (NSString *)curPage{
    return objc_getAssociatedObject(self, &curPageKey);
}
-(void)setCurPage:(NSString *)curPage{
    objc_setAssociatedObject(self, &curPageKey, curPage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static char pageSizeKey;
- (NSString *)pageSize{
    return objc_getAssociatedObject(self, &pageSizeKey);
}
- (void)setPageSize:(NSString *)pageSize{
    objc_setAssociatedObject(self, &pageSizeKey, pageSize, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static char totalPageKey;
- (NSString *)totalPage{
    return objc_getAssociatedObject(self, &totalPageKey);
}
- (void)setTotalPage:(NSString *)totalPage{
    objc_setAssociatedObject(self, &totalPageKey, totalPage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static char totalDataKey;
- (NSString *)totalData{
    return objc_getAssociatedObject(self, &totalDataKey);
}
- (void)setTotalData:(NSString *)totalData{
    objc_setAssociatedObject(self, &totalDataKey, totalData, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static char requestMsgKey;
- (NSString *)requestMsg{
    return objc_getAssociatedObject(self, &requestMsgKey);
}
- (void)setRequestMsg:(NSString *)requestMsg{
    objc_setAssociatedObject(self, &requestMsgKey, requestMsg, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static char dataMarrKey;
- (NSMutableArray *)dataMarr{
    return objc_getAssociatedObject(self, &dataMarrKey);
}

- (void)setDataMarr:(NSMutableArray *)dataMarr{
    objc_setAssociatedObject(self, &dataMarrKey, dataMarr, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark 刷新
- (void)add_jjHeadRefresh:(UIScrollView*)scrollView{
    
    //自定义控件 MJRefreshHeader 继承
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(load_jjNewData)];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    header.automaticallyChangeAlpha = YES;
    
    // 设置文字
    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [header setTitle:@"松手刷新" forState:MJRefreshStatePulling];
    [header setTitle:@"加载中 ..." forState:MJRefreshStateRefreshing];
    
    // 设置字体
    header.stateLabel.font = [UIFont systemFontOfSize:14];
    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:13];
    
    // 设置颜色
    header.stateLabel.textColor = [UIColor colorWithRed:133/255.0 green:133/255.0 blue:133/255.0 alpha:1];
    header.lastUpdatedTimeLabel.textColor = [UIColor colorWithRed:133/255.0 green:133/255.0 blue:133/255.0 alpha:1];
    // 设置header
    scrollView.mj_header = header;
    
    // 马上进入刷新状态
    //[self.collectionView.mj_header beginRefreshing];
    
    
}
#pragma mark  加载
- (void)add_jjFooterRefresh:(UIScrollView*)scrollView{
    if (scrollView.mj_footer) {
        return;
    }
    
    
    if ([scrollView isKindOfClass:[UITableView class]]) {
        UITableView *tableView = (UITableView *)scrollView;
        if (@available(iOS 11.0, *)) {
            tableView.estimatedRowHeight = 150;
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(load_jjMoreData)];
    // 隐藏刷新状态的文字
    footer.refreshingTitleHidden = YES;
    
    // 设置文字
    [footer setTitle:@"加载更多" forState:MJRefreshStateIdle];
    [footer setTitle:@"加载中~" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"———  没有更多了  ———" forState:MJRefreshStateNoMoreData];
    
    // 设置字体
    footer.stateLabel.font = [UIFont systemFontOfSize:14];
    
    // 设置颜色
    footer.stateLabel.textColor = [UIColor darkGrayColor];
    // 设置footer
    scrollView.mj_footer = footer;
    
}
- (void)setNoMoreData:(UIScrollView*)scrollView title:(NSString *)title {
    if (title.length) {
        if (!scrollView.mj_footer) {
            [self add_jjFooterRefresh:scrollView];
        } 
        [(MJRefreshAutoNormalFooter *)scrollView.mj_footer setTitle:title forState:MJRefreshStateNoMoreData];
        scrollView.mj_insetB = scrollView.mj_footer.mj_h = 70;
    }
}


/// 刷新数据
- (void)load_jjNewData{
    self.curPage = @"1";
    self.pageSize = @"15";
}

/// 加载数据
- (void)load_jjMoreData{
    self.curPage=[NSString stringWithFormat:@"%lld",[self.curPage longLongValue]+1];
}


@end
